<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;

class Index extends ActiveRecord
{
    public static function db() {
        return Yii::$app->db;
    }
    
    public static function tableName() {
        return 'house_pics';
    }
    /**
     * 获取首页图片列表
     * @param array $data
     */
    public function getList($data) {
        $limit = ($data['page'] -1) * $data['size'];
        $sql   = 'SELECT * FROM ' . self::tableName() . ' WHERE source=0 and status=0 ORDER BY orders DESC, tag,score,createtime DESC limit '. $limit . ',' . $data['size'];
        return self::db()->createCommand($sql)->queryAll();
    }
    
    
    /**
     * 添加用户反馈
     * @param unknown $data
     */
    public function addAdvice($data) {
        return self::db()->createCommand()
        ->insert('user_advice', [
            'uid'       => $data['uid'],
            'advice'    => $data['advice']
        ])->execute();
    }
    

    /**
     * 用户赞过的家居图数
     * @param unknown $uid
     */
    public function getPraiseNum($uid) {
        $sql = 'select count(*) from house_praise where uid=' . $uid;
        return self::db()->createCommand($sql)->queryScalar();
    }
    /**
     * 用户赞过的家居图
     * @param unknown $data
     * @return multitype:unknown
     */
    public function getPraiseList($data) {
        $uid   = $data['uid'];
        $page  = $data['page'];
        $size  = $data['size'];
        $limit = ($page - 1) * $size;
        $sql = 'select houseid from house_praise where uid=' . $uid . ' limit ' .$limit. ',' .$size;
        $houseIds = self::db()->createCommand($sql)->queryAll();
        $houses = array();
        foreach($houseIds as $value) {
            $houseId = $value['houseid'];
            $sql = "select id, description, uid, imgid from house_pics where id=$houseId";
            $house = self::db()->createCommand($sql)->queryOne();
            $sql = "select count(1) from house_praise where houseid=$houseId";
            $num = self::db()->createCommand($sql)->queryScalar();
            $house['praiseNum'] = $num;
            $houses[] = $house;
        }
        return $houses;
    }
}
